<%@page import="Proxies.CtrlConexionesWSService"%>
<%@page import="Proxies.CtrlConexionesWS"%>
<%@page import="Proxies.EnumTipoAplicacion"%>
<%@page import="java.net.InetAddress"%>
<%@page import="Proxies.DataConexion"%>
<%@page import="javax.xml.namespace.QName"%>
<%@page import="java.net.URL"%>
<%@page import="Proxies.EnumLogin"%>
<%@page import="Proxies.DataUsuario"%>
<%@page import="Proxies.CtrlUsuariosWS"%>
<%@page import="Proxies.CtrlUsuariosWSService"%>
<%

    String mail = request.getParameter("mail");
    String pass = request.getParameter("password");

    String IP = (String) session.getAttribute("SERVIDOR");
    URL dirUsr = new URL(IP + "CtrlUsuarios?wsdl");
    QName qNaUsr = new QName("http://WebServices/", "CtrlUsuariosWSService");
    CtrlUsuariosWS ctrlUsr = new CtrlUsuariosWSService(dirUsr, qNaUsr).getCtrlUsuariosWSPort();

    boolean existeUsr = ctrlUsr.containsKey(mail);
    boolean coincidePass = false;

    DataUsuario usr = new DataUsuario();

    if (existeUsr) {
        coincidePass = ctrlUsr.coincidePassUsr(mail, pass);
        if (coincidePass) {
            session.setAttribute("estado_sesion", EnumLogin.LOGIN_CORRECTO);
            usr = ctrlUsr.getUsuario(mail);
            ctrlUsr.cambioPassUsr(mail);
            session.setAttribute("usr", usr);
            response.sendRedirect("inicio.jsp");
            //Mandar conexion usuario.
            InetAddress address = null;
            address = InetAddress.getLocalHost();
            DataConexion dc = new DataConexion();
            dc.setIp(address.getHostAddress());
            dc.setTipoAplic(EnumTipoAplicacion.WEB);
            dc.setUsrLogueado(mail);
            URL dirConex = new URL(IP + "CtrlConexiones?wsdl");
            QName qNaConex = new QName("http://WebServices/", "CtrlConexionesWSService");
            CtrlConexionesWS ctrlConex = new CtrlConexionesWSService(dirConex, qNaConex).getCtrlConexionesWSPort();
            ctrlConex.agregarConexion(dc);
        }
    }
    
    if (!existeUsr || !coincidePass) {
        session.setAttribute("estado_sesion", EnumLogin.LOGIN_INCORRECTO);
        response.sendRedirect("index.jsp");
    }

%>
